﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using BIPorter.Common.Model;
using System.Web;
using BIPorter.Common.CommonFilter;

namespace BIPorter.Common.BLL
{
     public class MenuBll
    {
        #region 私有方法

        /// <summary>
        /// 从配置文件获取 菜单权限关系
        /// </summary>
        /// <returns></returns>
         public List<MeunModel> GetMenuList(string userRoler=null)
         {
             var path = PathHelper.GetSettings("MenuConfig");
             XDocument xdoc = XDocument.Load(path);

             var list = new List<MeunModel>();
             var Meuns = xdoc.Elements("Meuns").FirstOrDefault();
             foreach (var child in Meuns.Elements("Meun"))
             {
                 int id = Convert.ToInt16(child.Element("Id").Value);
                 int? parentId = Convert.ToInt16(child.Element("ParentId").Value);
                 string displayName = child.Element("DisplayName").Value;
                 string actionName = child.Element("ActionName").Value;
                 string controllerName = child.Element("ControllerName").Value;
                 string server = child.Element("Server").Value;
                 string roles = child.Element("Roles").Value;

                 if (string.IsNullOrEmpty(userRoler))//不指定角色
                 {
                     var meunModel = new MeunModel(id, displayName, actionName, controllerName, server, roles, parentId);
                     list.Add(meunModel);
                 }
                 else if (!string.IsNullOrEmpty(roles))//特定角色
                 {
                     var t = roles.Split(',');
                     if (t.Any(u => u == userRoler))
                     {
                         var meunModel = new MeunModel(id, displayName, actionName, controllerName, server, roles, parentId);
                         list.Add(meunModel);
                     }
                 }
             }
             return list;
         }


         /// <summary>
         /// 带有层级关系的菜单
         /// </summary>
         /// <param name="list"></param>
         /// <returns></returns>
         public List<MeunModel> GetShowMenuList(string userRoler=null)
         {
             var list = this.GetMenuList(userRoler);
             var res = new List<MeunModel>();
             if (list.Any())
             {
                 res = list.FindAll(l => l.ParentId == 0);
                 res.ForEach(r => r.ChildMenus =
                 list.FindAll(l => l.ParentId == r.Id && l.ParentId > 0));
             }
             return res;
         }


         /// <summary>
         /// 判断角色是否支持该操作
         /// </summary>
         /// <returns></returns>
         public bool IsLegl(FilterContextInfo f, string roler)
         {
             bool res = false;
             var list = this.GetMenuList(roler);
             var list2 = this.GetMenuList();
             if (!list2.Any(l => (l.ActionName == f.actionName && l.ControllerName == f.controllerName && l.Server == f.domainName)))
             {
                 return true; //未加配置文件 的默认都支持
             }
             res  = list.Any(l=>(l.ActionName==f.actionName&&l.ControllerName==f.controllerName));
             return res;
         }

        #endregion
    }
}
